<?php

namespace app\controllers;
use srvs\ContentDetailService;
use srvs\ContentService;
use srvs\MenuService;
use yii\web\NotFoundHttpException;

class HomeController extends BaseController
{
    public function actionMetadatas(){
        $response = \Yii::$app->response;
        $response->format =  $response::FORMAT_JSON ;
        $response->data =["result"=>[]];
        return $response;
    }

    public function actionIndex()
    {
        $MenuService=new MenuService();
        $ContentService=new ContentService();

        $text_menu=$MenuService->getOneById("58c016c68ac24700635eb148");
        $text_content_list=$ContentService->get_data_for_show($text_menu,0,4)["content_list"];

        $pic_menu=$MenuService->getOneById("58c016ef2f301e006ba3baff");
        $pic_content_list=$ContentService->get_data_for_show($pic_menu,0,8)["content_list"];

        \Yii::$app->view->params['top_menu_active_id']="0";
        return $this->render('index',["text_menu"=>$text_menu,"text_content_list"=>$text_content_list,"pic_menu"=>$pic_menu,"pic_content_list"=>$pic_content_list]);
    }

    public function actionMenuLeftRight(){
        $menu_name=I("s");
        $page=I("page",0);
        if(empty($menu_name)){
            throw new NotFoundHttpException();
        }
        $MenuService=new MenuService();
        $menu=$MenuService->getOne(["menu_name"=>$menu_name]);
        if(empty($menu)){
            throw new NotFoundHttpException();
        }
        $object_id=$menu["objectId"];
        $top_menu_id=empty($menu["parent_id"])?$object_id:$menu["parent_id"];
        $top_menu=empty($menu["parent_id"])?$menu:$MenuService->getOneById($menu["parent_id"]);
        $children_menu=$MenuService->getAll(["parent_id"=>$top_menu_id],["menu_sort asc"]);

        $ContentService=new ContentService();
        $right_content_list=$ContentService->get_data_for_show($menu,$page,12);//右侧显示内容
        $right_content_from_menu=$menu;//右侧显示内容对应的菜单
        $top_menu_active_id=0;//顶部导航选中菜单
        $left_meun_active_id=0;//左侧导航选中菜单
        if(empty($menu["parent_id"])){
            $top_menu_active_id=$object_id;
            if(empty($right_content_list["content_list"])&&!empty($children_menu)){
                $right_content_list=$ContentService->get_data_for_show(current($children_menu),$page,12);
                $left_meun_active_id=current($children_menu)["objectId"];
                $right_content_from_menu=current($children_menu);
            }
        }
        else{
            $top_menu_active_id=$menu["parent_id"];
            $left_meun_active_id=$object_id;
        }
        \Yii::$app->view->params['top_menu_active_id']=$top_menu_active_id;
        \Yii::$app->view->params['left_meun_active_id']=$left_meun_active_id;

        $newest_content=$ContentService->getAll(["status"=>0],["createdAt desc"],5);

        $data=[
            "top_menu"=>$top_menu,
            "children_menu"=>$children_menu,
            "right_content_list"=>$right_content_list,
            "right_content_from_menu"=>$right_content_from_menu,
            "newest_content"=>$newest_content,
        ];
        return $this->render('menu-left-right',$data);
    }

    public function actionMenu(){
        $menu_name=I("s");
        $page=I("page",0);
        if(empty($menu_name)){
            throw new NotFoundHttpException();
        }
        $MenuService=new MenuService();
        $menu=$MenuService->getOne(["menu_name"=>$menu_name]);
        if(empty($menu)){
            throw new NotFoundHttpException();
        }
        $object_id=$menu["objectId"];
        $top_menu_id=empty($menu["parent_id"])?$object_id:$menu["parent_id"];
        $top_menu=empty($menu["parent_id"])?$menu:$MenuService->getOneById($menu["parent_id"]);
        $children_menu=$MenuService->getAll(["parent_id"=>$top_menu_id],["menu_sort asc"]);
        $pagesize=8;
        if($top_menu["menu_type"]==1){
            $pagesize=9;
        }
        $ContentService=new ContentService();
        if(empty($menu["parent_id"])&&!empty($children_menu)){
            $content_list=$ContentService->get_data_for_show(current($children_menu),$page,$pagesize);
            $sub_meun_active_id=current($children_menu)["objectId"];
        }
        else{
            $content_list=$ContentService->get_data_for_show($menu,$page,$pagesize);
            $sub_meun_active_id=$object_id;
        }

        \Yii::$app->view->params['top_menu_active_id']=$top_menu_id;
        \Yii::$app->view->params['sub_meun_active_id']=$sub_meun_active_id;

        $data=[
            "top_menu"=>$top_menu,
            "children_menu"=>$children_menu,
            "content_list"=>$content_list,
        ];
        return $this->render('menu',$data);
    }

    public function actionContent(){
        $object_id=I("id");
        if(empty($object_id)){
            throw new NotFoundHttpException();
        }
        $ContentService=new ContentService();
        $MenuService=new MenuService();
        $ContentDetailService=new ContentDetailService();
        $content=$ContentService->getOneById($object_id);
        if(empty($content)){
            throw new NotFoundHttpException();
        }
        $content_detail=current($ContentDetailService->getAll(["content_id"=>$object_id]));
        $content["detail"]=$content_detail["detail"];
        $menu=$MenuService->getOneById($content["menu_id"]);
        if(empty($menu["parent_id"])){
            \Yii::$app->view->params['top_menu_active_id']=$content["menu_id"];
        }
        else{
            \Yii::$app->view->params['top_menu_active_id']=$menu["parent_id"];
        }
        $ContentService->update(["objectId"=>$content["objectId"],"hits"=>(int)$content["hits"]+1]);
        return $this->render('content',["content"=>$content]);
    }
}